package com.bianguo.android.apushdemo.utils;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.bianguo.android.apushdemo.dao.DaoMaster;
import com.bianguo.android.apushdemo.dao.DaoSession;
import com.bianguo.android.apushdemo.dao.ImagesDao;
import com.bianguo.android.apushdemo.dao.MovieEntityDao;
import com.bianguo.android.apushdemo.dao.RatingDao;
import com.bianguo.android.apushdemo.dao.SubjectDao;
import com.bianguo.android.apushdemo.entity.Images;
import com.bianguo.android.apushdemo.entity.MovieEntity;
import com.bianguo.android.apushdemo.entity.Rating;
import com.bianguo.android.apushdemo.entity.Subject;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/9/8.
 */

public class DBManager {

    private static final String DB_NAME = "db_movie";
    private static DBManager dbManager;
    private DaoMaster.DevOpenHelper openHelper;
    private Context mContext;

    private DBManager(Context mContext) {
        this.mContext = mContext;
        openHelper = new DaoMaster.DevOpenHelper(mContext, DB_NAME, null);
    }

    public static DBManager getInstance(Context mContext) {
        synchronized (DBManager.class){
            if (dbManager == null) {
                synchronized (DBManager.class) {
                    if (dbManager == null) {
                        dbManager = new DBManager(mContext);
                    }
                }
            }
        }
        return dbManager;
    }

    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, DB_NAME, null);
        }
        return openHelper.getReadableDatabase();
    }

    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(mContext, DB_NAME, null);
        }
        return openHelper.getWritableDatabase();
    }

    public void insertMovie(MovieEntity movieEntity) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        MovieEntityDao movieEntityDao = daoSession.getMovieEntityDao();
        movieEntityDao.insert(movieEntity);

        insertSubjects(movieEntity.getSubjects());
    }

    public MovieEntity getMovie() {
        MovieEntity movieEntity = new MovieEntity();
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        MovieEntityDao movieEntityDao = daoSession.getMovieEntityDao();
//        MovieEntity daoMovieEntity = movieEntityDao.loadByRowId(0);
        List<MovieEntity> list = movieEntityDao.queryBuilder().build().list();
        MovieEntity daoMovieEntity = list.get(0);
        MyLog.mLog("list.size() = " + list.size());
        movieEntity.setCount(daoMovieEntity.getCount());
        movieEntity.setStart(daoMovieEntity.getStart());
        movieEntity.setTitle(daoMovieEntity.getTitle());
        movieEntity.setTotal(daoMovieEntity.getTotal());
        movieEntity.setSubjects((ArrayList<Subject>) getSubjects());
        return movieEntity;
    }

    private void insertSubjects(ArrayList<Subject> subjects) {
        if (subjects == null || subjects.isEmpty()) {
            return;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        SubjectDao subjectDao = daoSession.getSubjectDao();
        subjectDao.insertInTx(subjects);

        int size = subjects.size();
        for (int i = 0; i < size; i++) {
            Subject subject = subjects.get(i);
            insertRating(subject.getRating());
            insertImages(subject.getImages());
        }
    }

    private void insertRating(Rating rating) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        RatingDao ratingDao = daoSession.getRatingDao();
        ratingDao.insert(rating);
    }

    private void insertImages(Images images) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        ImagesDao imagesDao = daoSession.getImagesDao();
        imagesDao.insert(images);
    }

    private List<Subject> getSubjects() {
        SubjectDao subjectDao = new DaoMaster(getReadableDatabase()).newSession().getSubjectDao();
        List<Subject> subjects = subjectDao.loadAll();
        return subjects;
    }

    private Rating getRating() {
        RatingDao ratingDao = new DaoMaster(getReadableDatabase()).newSession().getRatingDao();
        return null;
    }

    private Images getImages() {
        ImagesDao imagesDao = new DaoMaster(getReadableDatabase()).newSession().getImagesDao();
        return null;
    }
}
